<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

class ProfessionCourse extends Base
{

    /**
     * 课程详情
     * @return Json
     * @throws BusinessException
     */
    public function getInfo()
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交查询
        $response = app(\app\api\logic\ProfessionCourse::class)->getInfo($param);

        // 返回
        return $this->response($response);
    }


    /**
     * 选择专业课程列表
     * @return Json
     * @throws BusinessException
     */
    public function getList(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'professions_id' => 'require',
        ];

        //错误信息
        $errMsg = [
            'professions_id.require' => '专业id不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交查询
        $response = app(\app\api\logic\ProfessionCourse::class)->getList($param);

        //返回
        return $this->response($response);
    }

    /**
     * 加入课程
     * @throws BusinessException
     */
    public function join()
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'join' => 'require',
            'course_id' => 'require',
        ];

        //错误信息
        $errMsg = [
            'join.require' => '类型join不得为空',
            'course_id.require' => '课程course_id不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        //提交操作
        app(\app\api\logic\ProfessionCourse::class)->join($param);

        //返回
        return $this->response();
    }

    /**
     * 查询课程题型信息
     * @throws BusinessException
     */
    public function getQuestionType(): Json
    {
        //外部参数
        $param = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
            'type.require' => '来源类型type不得为空',
        ];

        //数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        //提交查询
        $response = app(\app\api\logic\Question::class)->getQuestionType($param);

        //返回
        return $this->response($response);
    }

    /**
     * 查询课程题目(每页20道题)
     * @throws BusinessException
     */
    public function get_topic(): Json
    {
        //外部参数
        $params = $this->request->param();

        //验证规则
        $rule = [
            'id' => 'require',
            'type' => 'require',
        ];

        //错误信息
        $errMsg = [
            'id.require' => '课程id不得为空',
            'type.require' => '课程题型type不得为空',
        ];

        //数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        //提交查询
        $response = app(\app\api\logic\Question::class)->get_topic_type($params);

        //返回
        return $this->response($response);
    }

    /**
     * 搜索
     * @throws BusinessException
     */
    public function search(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'keyword' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'keyword.require' => '关键字keyword不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交查询
        $response = app(\app\api\logic\ProfessionCourse::class)->search($param);

        // 返回
        return $this->response($response);
    }
}